var express = require("express");
var router = express.Router();
const { v4: uuidv4 } = require("uuid");
const db = require("../db/index.js");
router.get("/list", function (req, res, next) {
  console.log(req.query);
  // 获取参数   当前页码   当前每页数据量
  // 获取参数   当前页码   当前每页数据量
  let pageNumber = req.query.pageNumber;
  // let pageSize = parseInt(req.body.pageSize)
  let pagesize = req.query.pagesize - 0;
  // 求得sql语句中的参数
  console.log(pageNumber, pagesize);
  //  if(!pageNumber) pageNumber=1
  //  if(!pagesize) pagesize=6
  console.log(pageNumber, pagesize);
  let start = (pageNumber - 1) * pagesize;
  // sql语句
  let sql = "select * from exam limit ?, ?";
  let sqlParam = [start, pagesize];
  let sqlCount = "select count(*) from exam";

  var count = 0;
  // 执行查询
  db.query(sqlCount, null, (err, result) => {
    // 得到总记录数
    count = result[0]["count(*)"];
  });

  db.query(sql, sqlParam, (err, result) => {
    if (err) {
      return res.send({
        code: 0,
        message: err.message,
        ok: true,
      });
    } else {
      return res.send({
        code: 200,
        message: "请求成功",
        ok: true,
        totalCount: count,
        data: result,
      });
    }
  });
});
router.post("/addExam", function (req, res, next) {
  let info = req.body;
  const sql1 = `insert into exam set ?`;
  db.query(
    sql1,
    {
      type: info.type,
      img: info.img,
      adress: info.adress,
      time: info.time,
      school: info.school,
    },
    function (err, data) {
      if (err) {
        return res.send({ code: 0, message: err.message });
      }
      res.send({ code: 200, message: "添加成功！" });
    }
  );
});
// 删除预约考试
router.post("/deleteExam", function (req, res, next) {
  let id = req.query.id;
  const sql = "delete from exam where id=?";
  db.query(sql, id, function (err, result) {
    if (err) {
      return res.send({ code: 0, message: err.message });
    } else {
      res.send({ code: 200, message: "删除成功！" });
    }
  });
});
// 获得考试详细信息
router.get("/getInfo", function (req, res) {
  console.log(req.query);
  let id = req.query.id;
  const sql = `select * from exam where id=?`;
  db.query(sql, id, function (err, results) {
    if (err) {
      return res.send({ code: 0, message: err.message });
    } else {
      return res.send({ code: 200, message: "查询成功", data: results[0] });
    }
  });
});

//   点击报名
router.post("/takeExam", function (req, res, next) {
  let info = req.body;
  const sql = `select * from userExam where username=?`;
  db.query(sql, info.username, function (err, results) {
    if (err) {
      return res.send({ code: 0, message: err.message });
    }
    if (results.length > 0) {
      return res.send({ code: 0, message: "你已经报考一门科目了！" });
    }
    const sql1 = `insert into userExam set ?`;
    let ID = uuidv4();
    ID = ID.replace(/-/g, "");
    db.query(
      sql1,
      {
        id: ID,
        username: info.username,
        nickname: info.nickname,
        type: info.type,
        img: info.img,
        adress: info.adress,
        time: info.time,
        school: info.school,
      },
      function (err, data) {
        if (err) {
          return res.send({ code: 0, message: err.message });
        }
        res.send({ code: 200, message: "预约成功！" });
      }
    );
  });
});
// 取消考试内容
router.post("/cancelExam", function (req, res, next) {
  let id = req.query.id;
  const sql = "delete from userexam where id=?";
  db.query(sql, id, function (err, result) {
    if (err) {
      return res.send({ code: 0, message: err.message });
    } else {
      res.send({ code: 200, message: "取消成功！" });
    }
  });
});

module.exports = router;
